package ostt1_TokenLib;

import iaik.pkcs.pkcs11.TokenException;
import iaik.pkcs.pkcs11.wrapper.PKCS11Exception;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class Ostt1TokenService {
	
	// Constant
	public static final int SUCCESS = 0;
	public static final int FAILED = -1;
	
	/**
	 * Constructor
	 */
	public Ostt1TokenService(){
	}
	
	@WebMethod
	public long connectService(){
		Ostt1TokenLib.p11initialize();
		long session = Ostt1TokenLib.connectToken();
		return session;
	}
	
	@WebMethod
	public int disconnectService(){
		Ostt1TokenLib.p11initialize();
		Ostt1TokenLib.connectToken();
		Ostt1TokenLib.disconnectToken();
		return SUCCESS;
	}
	
	@WebMethod
	public int loginService(String password){
		Ostt1TokenLib.p11initialize();
		Ostt1TokenLib.connectToken();
		if (Ostt1TokenLib.Login(password.toCharArray())){
			return SUCCESS;
		} else {
			return  FAILED;
		}
	}
	
	@WebMethod
	public int logoutService(){
		Ostt1TokenLib.p11initialize();
		Ostt1TokenLib.connectToken();
		if (Ostt1TokenLib.Logout()){
			return SUCCESS;
		} else {
			return  FAILED;
		}
	}
	
	@WebMethod
	public int changeUserPinService(String oldpass, String newpass){
		Ostt1TokenLib.p11initialize();
		Ostt1TokenLib.connectToken();
		if (Ostt1TokenLib.ChangeUserPIN(oldpass.toCharArray(), newpass.toCharArray())){
			return SUCCESS;
		} else {
			return  FAILED;
		}
	}

	@WebMethod
	public int changeSOPinService(String oldSOpass, String newSOpass){
		Ostt1TokenLib.p11initialize();
		Ostt1TokenLib.connectToken();
		if (Ostt1TokenLib.ChangeSOPIN(oldSOpass.toCharArray(), newSOpass.toCharArray())){
			return SUCCESS;
		} else {
			return  FAILED;
		}
	}

	@WebMethod
	public int unblockPinService(String SOpass, String newpass){
		Ostt1TokenLib.p11initialize();
		Ostt1TokenLib.connectToken();
		if (Ostt1TokenLib.UnblockPIN(SOpass.toCharArray(), newpass.toCharArray())){
			return SUCCESS;
		} else {
			return  FAILED;
		}
	}

	@WebMethod
	public String getEnumCertService(int certNo, int certAttr){
		Ostt1TokenLib.p11initialize();
		Ostt1TokenLib.connectToken();
		String certID = null;
		try {
			certID = Ostt1TokenLib.GetEnumCer(certNo, certAttr);
		} catch (PKCS11Exception e) {
		}
		return certID;
	}

	@WebMethod
	public String[] getCertificateIDService(){
		String[] certID = null;
		Ostt1TokenLib.p11initialize();
		Ostt1TokenLib.connectToken();
		try {
			certID = Ostt1TokenLib.GetCertificateID();
		} catch (PKCS11Exception e) {
		}
		return certID;
	}

	@WebMethod
	public String getEnumPubService(int certNo){
		String objID = null;
		Ostt1TokenLib.p11initialize();
		Ostt1TokenLib.connectToken();
		try {
			objID = Ostt1TokenLib.GetEnumPub(certNo);
		} catch (PKCS11Exception e) {
		}
		return objID;
	}

	@WebMethod
	public String getEnumPriService(int certNo){
		String objID = null;
		Ostt1TokenLib.p11initialize();
		Ostt1TokenLib.connectToken();
		try {
			objID = Ostt1TokenLib.GetEnumPri(certNo);
		} catch (TokenException e) {
		}
		return objID;
	}
}